package com.ssm.supplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ssm.common.utils.JWTUtil;
import com.ssm.common.utils.PageUtils;
import com.ssm.common.utils.R;
import com.ssm.supplier.entity.SsmSupplierEntity;
import com.ssm.supplier.service.SsmSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * 
 *
 * @author ssm
 * @email sunlightcs@gmail.com
 * @date 2023-03-19 17:27:39
 */
@RestController
@RequestMapping("supplier/ssmsupplier")
public class SsmSupplierController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SsmSupplierService ssmSupplierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ssmSupplierService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{ghId}")
    public R info(@PathVariable("ghId") Integer ghId){
		SsmSupplierEntity ssmSupplier = ssmSupplierService.getById(ghId);

        return R.ok().put("ssmSupplier", ssmSupplier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SsmSupplierEntity ssmSupplier){
		ssmSupplierService.save(ssmSupplier);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody SsmSupplierEntity ssmSupplier){
		ssmSupplierService.updateById(ssmSupplier);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ghIds){
		ssmSupplierService.removeByIds(Arrays.asList(ghIds));

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody SsmSupplierEntity supplier) {
        String user = supplier.getGhUser();
        //获取供货端登录密码
        String password = supplier.getGhPwd();
        //获取供货端手机号
        String phone = supplier.getGhPhone();

        //构造条件查询构造对象
        LambdaQueryWrapper<SsmSupplierEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SsmSupplierEntity::getGhUser,user );
        SsmSupplierEntity one = ssmSupplierService.getOne(queryWrapper);
        //判断用户是否存在
        if (one==null){
            return R.error(401,"用户名不存在");
        }
        //判断用户名或者密码是否为空
        if (StringUtils.isEmpty(supplier.getGhUser()) || StringUtils.isEmpty((supplier.getGhPwd()))  ){
            return R.error(402,"用户名或密码不能为空");
        }
        //判断是否在合法的前提下是否正确
        if (!one.getGhPwd().equals(password) && supplier.getGhPwd().length()>=6 &&supplier.getGhPwd().length()<=12) {
            return R.error(403,"密码不正确");
        }
        //判断密码是否合法
        if (supplier.getGhPwd().length()<6 || supplier.getGhPwd().length()>12){
            return R.error(404,"密码不合法");
        }
        //生成token
        String token= JWTUtil.generateToken(supplier.getGhPhone()+supplier.getGhUser());
        //生成accessToken
        String accessToken = UUID.randomUUID().toString().replace("-", "");
        //将token和user存入redis
        redisTemplate.opsForHash().put(accessToken, "token", token);
        redisTemplate.opsForHash().put(accessToken,"user",user);
        //设置token过期时间
        redisTemplate.expire(accessToken, JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MINUTES);
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        map.put("data", one);
        map.put("token", token);
        map.put("user",user);

        return R.ok(map);

    }

    /**
     * 供货app修改密码
     * @param supplier
     * @return
     */
    @PutMapping("/updateSecret")
    public R updateSecret(@RequestBody SsmSupplierEntity supplier){
        ssmSupplierService.updateById(supplier);
        return R.ok();
    }



}
